package com.happy.Animation.controllers;

import com.happy.Animation.dao.ClassificationRepository;
import com.happy.Animation.entities.Classification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("classifications")
public class ClassificationController {

    @Autowired
    ClassificationRepository classificationRepository;

    @GetMapping
    public String displayClassifications(Model model){
        List<Classification> classifications = classificationRepository.findAll();
        model.addAttribute("classifications",classifications);
        return "classifications/list-classifications";
    }

    @GetMapping("/new")
    public String displayClassificationForm(Model model){

        Classification aClassification = new Classification();
        model.addAttribute("classification",aClassification);
        return "classifications/new-classification";
    }
    @PostMapping("/save")
    public String createClassification(Model model,Classification classification){
        classificationRepository.save(classification);

        return "redirect:/classifications/new";
    }
}
